	
	package servlet.user;
	
	import java.io.IOException;
	import java.util.ArrayList;
	import java.util.List;
	import javax.servlet.ServletException;
	import javax.servlet.http.HttpServlet;
	import javax.servlet.http.HttpServletRequest;
	import javax.servlet.http.HttpServletResponse;
	
	import service.UserManager;
	import enitys.User;
	
	public class AddUser extends HttpServlet {
	
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/**
		 * Constructor of the object.
		 */
		public AddUser() {
			super();
		}
	
		/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
	
				doPost(request,response);
		}
	
		/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
	
			String name = request.getParameter("name");
			String pass = request.getParameter("password");
			String email = request.getParameter("email");
			String Class = request.getParameter("class");
			List<String> all = new ArrayList<String>();// 保存返回的信息
			User user = new User();
			user.setUsername(name);
			user.setPassword(pass);
			user.setEmail(email);
			user.setClasses(Class);
			boolean flag = UserManager.getInstance().register(user);
			if (!flag) {
				all.add("该用户已经存在了！");
			} else {
				all.add("用户添加成功");
			}
			request.setAttribute("all", all);// 保存错误信息
			request.getRequestDispatcher("/admin.jsp").forward(request, response);
		}
	
	}
